package avicit.bdp.dgs.standard.rest;

import avicit.bdp.dgs.standard.dto.StandardDocDto;
import avicit.bdp.dgs.standard.service.StandardDocService;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 数据标准文档管理
 *
 * @author mayi
 */
@RestController
@RequestMapping("/api/bdp/dgs/dataStandard/doc/")
@Api(tags = "数据标准文档管理")
public class StandardDocRest {

  private final StandardDocService standardDocService;

  @Autowired
  public StandardDocRest(StandardDocService standardDocService) {
    this.standardDocService = standardDocService;
  }

  @ApiOperation(value = "分页查询")
  @ApiImplicitParams({
    @ApiImplicitParam(value = "分类ID", name = "typeId", dataType = "String", paramType = "query"),
    @ApiImplicitParam(value = "查询关键词", name = "keyWords", dataType = "String", paramType = "query"),
    @ApiImplicitParam(value = "状态", name = "status", dataType = "Int", paramType = "query"),
    @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int", paramType = "query"),
    @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int", paramType = "query")
  })
  @GetMapping
  public ResponseMsg<QueryRespBean<StandardDocDto>> getPageList(
      @RequestParam(required = false) String typeId,
      @RequestParam(required = false) String keyWords,
      @RequestParam(required = false) Integer status,
      @RequestParam(defaultValue = "1") Integer pageNo,
      @RequestParam(defaultValue = "10") Integer pageSize)
      throws Exception {

    ResponseMsg<QueryRespBean<StandardDocDto>> responseMsg = new ResponseMsg<>();

    QueryRespBean<StandardDocDto> queryRespBean =
        standardDocService.getPageList(typeId, keyWords, status, pageNo, pageSize);

    responseMsg.setResponseBody(queryRespBean);
    return responseMsg;
  }

  @ApiOperation(value = "新增")
  @ApiImplicitParams({
    @ApiImplicitParam(
        value = "分类ID",
        name = "typeId",
        dataType = "String",
        paramType = "form",
        required = true),
    @ApiImplicitParam(
        value = "文件编号",
        name = "code",
        dataType = "String",
        paramType = "form",
        required = true),
    @ApiImplicitParam(
        value = "文件名称",
        name = "name",
        dataType = "String",
        paramType = "form",
        required = true),
    @ApiImplicitParam(value = "文件说明", name = "remark", dataType = "String", paramType = "form"),
    @ApiImplicitParam(
        value = "文件",
        name = "file",
        dataType = "__file",
        paramType = "form",
        required = true)
  })
  @PostMapping
  public ResponseMsg<String> save(
      @RequestParam String typeId,
      @RequestParam String code,
      @RequestParam String name,
      @RequestParam(required = false) String remark,
      @RequestParam(required = false) String secretLevel,
      @RequestParam(value = "file") MultipartFile file) {
    ResponseMsg<String> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(standardDocService.save(typeId, code, name, remark, secretLevel, file));
    return responseMsg;
  }

  @ApiOperation(value = "修改")
  @ApiImplicitParams({
    @ApiImplicitParam(
        value = "id",
        name = "id",
        dataType = "String",
        paramType = "form",
        required = true),
    @ApiImplicitParam(
        value = "文件编号",
        name = "code",
        dataType = "String",
        paramType = "form",
        required = true),
    @ApiImplicitParam(
        value = "文件名称",
        name = "name",
        dataType = "String",
        paramType = "form",
        required = true),
    @ApiImplicitParam(value = "文件说明", name = "remark", dataType = "String", paramType = "form"),
    @ApiImplicitParam(value = "文件", name = "file", dataType = "__file", paramType = "form")
  })
  @PutMapping
  public ResponseMsg<Boolean> update(
      @RequestParam String id,
      @RequestParam String code,
      @RequestParam String name,
      @RequestParam String remark,
      @RequestParam(value = "file", required = false) MultipartFile file) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    standardDocService.update(id, code, name, remark, file);
    responseMsg.setResponseBody(true);
    return responseMsg;
  }

  @ApiOperation(value = "批量删除")
  @ApiParam(value = "逗号分隔的id串", name = "ids", required = true)
  @DeleteMapping
  public ResponseMsg<Boolean> batchDelete(@RequestBody String ids) throws IOException {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(standardDocService.batchDelete(ids));
    return responseMsg;
  }

  @ApiOperation(value = "通过ID查询")
  @ApiParam(value = "id", name = "id", required = true)
  @GetMapping("/{id}")
  public ResponseMsg<StandardDocDto> queryById(@PathVariable String id) {
    ResponseMsg<StandardDocDto> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(standardDocService.selectByPrimaryKey(id));
    return responseMsg;
  }

  @ApiOperation(value = "下载")
  @ApiParam(value = "id", name = "id", required = true)
  @GetMapping("/download/{id}")
  public void downLoad(
      @PathVariable String id, HttpServletRequest request, HttpServletResponse response) throws Exception {
    standardDocService.downLoad(id, request, response);
  }
}
